﻿// Copyright (c) 2006 Antu Corporation。 
// 创建人  ：LinJian
// 创建时间：2007年5月23日
using System;
using System.Collections.Generic;
using System.Text;
using Antu.Business.Struct;
using System.Data;
using Antu.Business.Common;

namespace Antu.Business.Admin
{
    /// <!--
    /// 功能描述  : 用户所属组的操作
    /// 创建人  : LinJian
    /// 创建时间: 2007年5月23日
    /// -->
    public class Group
    {
        /// <summary>
        /// 根据组id删除组
        /// </summary>
        /// <param name="strGroupId">用户组id</param>
        /// <returns></returns>
        public static int DeleteGroupById(string strGroupId)
        {
            string strSql = string.Format("delete from st_group where groupid='{0}'",
                strGroupId);
            return SysParams.OAConnection().RunSql(strSql);
        }

        /// <summary>
        /// 根据Id更新组信息
        /// </summary>
        /// <param name="grpsParam">存放需要更新的组信息</param>
        /// <returns></returns>
        public static int UpdateGroupById(GroupStruct grpsParam)
        {
            string strSql = string.Format("update st_group set GROUP_NAME='{0}' where groupid='{1}'",
                grpsParam.GroupName, grpsParam.GroupId);
            return SysParams.OAConnection().RunSql(strSql);
        }

        /// <summary>
        /// 根据组id获取组信息
        /// </summary>
        /// <param name="strGroupId">用户组id</param>
        /// <returns></returns>
        public static GroupStruct GetGroupById(string strGroupId)
        {
            GroupStruct grpsReturn = new GroupStruct();

            string strSql = string.Format("select GROUP_NAME from st_group where groupid='{0}'",
                strGroupId);

            grpsReturn.GroupId = strGroupId;
            grpsReturn.GroupName = SysParams.OAConnection().GetValue(strSql);

            return grpsReturn;
        }

        /// <summary>
        /// 添加组信息
        /// </summary>
        /// <param name="strGroupName">组名称</param>
        /// <param name="strGroupDescrip">组描述</param>
        /// <returns></returns>
        public static int AddGroup(string strGroupName,string strGroupDescrip)
        {
            string strSql = string.Format("Insert into st_group(GROUPID,GROUP_NAME,MEMO) values('{0}','{1}','{2}')",
                GenerateGuid.NewGuid,strGroupName,strGroupDescrip);
            return SysParams.OAConnection().RunSql(strSql);
        }

        /// <summary>
        /// 根据流程名和岗位名获取组号
        /// </summary>
        /// <param name="strWorkName">流程名</param>
        /// <param name="strStepName">岗位名</param>
        /// <returns></returns>
        public static String GetGroupIdOfStep(String strWorkName, String strStepName)
        {
            WorkFlow.ClsUserWorkFlow clsWorkFlow = new WorkFlow.ClsUserWorkFlow();
            string strWid = clsWorkFlow.GetFlowIdByName(strWorkName);

            String sql = string.Format("select GID from st_group_in_step where wid='{0}' "+
                "and stpname='{1}'", strWid, strStepName);
            string strGroupId = SysParams.OAConnection().GetValue(sql);

            return strGroupId;
        }

        /// <summary>
        /// 获取所有组信息
        /// </summary>
        /// <returns></returns>
        public static List<GroupStruct> GetAllGroupInfo(string groupName)
        {
            string strSql = "select * from st_group ";
            if (!String.IsNullOrEmpty(strSql))
                strSql += " WHERE Group_Name like '%" + groupName + "%'";
            DataTable dtPost;
            SysParams.OAConnection().RunSql(strSql, out dtPost);

            List<GroupStruct> lstGroup = new List<GroupStruct>();
            foreach (DataRow drTemp in dtPost.Rows)
            {
                GroupStruct gsTemp = new GroupStruct();
                gsTemp.GroupId = drTemp["GROUPID"].ToString();
                gsTemp.GroupName = drTemp["GROUP_NAME"].ToString();
                lstGroup.Add(gsTemp);
            }

            return lstGroup;
        }
    }
}
